package com.cc.bsp.persistence.entity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.cc.bsp.persistence.IOrder;

public class BasicOrderEntity implements IOrder, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2658020216170996491L;
	
	
	public Map<String,String> orderProperties = new HashMap<String,String>();
	
	private TYPE type = IOrder.TYPE.DESC;

	public void add(TYPE type, Object... propertyNames) {
		
		orderProperties.clear();
		
		int  index = orderProperties.size();
	
		for (Object propertyName : propertyNames) {
			orderProperties.put(Integer.toString(index), (String) propertyName);
			break;
		}
	}

	public String getOrderSQL() {
		
		if (orderProperties == null || orderProperties.isEmpty()) {
			return "";
		}
		
		Iterator<String> i = orderProperties.keySet().iterator();

		StringBuffer sb = new StringBuffer();

		sb.append(" order by ");
		
		int count = 0;
		
		while (i.hasNext()) {
			
			String key = i.next();
			
			String propertyName = orderProperties.get(key);
			
			if (count == 0) {
				sb.append(propertyName);
			} else {
				sb.append(" , ").append(propertyName);
			}
			
			count = count + 1;
			
		}

		if (IOrder.TYPE.DESC == type) {
			sb.append(" desc ");
		} else {
			sb.append(" asc ");
		}
		
		return sb.toString();
	}

	public Map<String, String> getOrderProperties() {

		return orderProperties;
	}

	public TYPE getType() {

		return type;
	}

}
